#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size();
        int left = 0;
        int right = n - 1;
        int mid = 0;
        sort(nums.begin(), nums.end());
        while (left < right)
        {
            mid = left + (right - left) / 2;
            if (nums[mid] == mid)
                left = mid + 1;
            else
                right = mid;
        }

        if (right == n - 1)
            return nums[right] == right ? right + 1 : right;

        return right;
    }
};